package org.jeecg.modules.tls.blog.article.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import io.micrometer.core.instrument.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.tls.blog.article.entity.AddArticleDto;
import org.jeecg.modules.tls.blog.article.entity.AddBlogDto;
import org.jeecg.modules.tls.blog.article.entity.BlogArticle;
import org.jeecg.modules.tls.blog.article.mapper.BlogArticleMapper;
import org.jeecg.modules.tls.blog.article.service.IBlogArticleService;

import org.jeecg.modules.tls.blog.article.vo.ArticleDetailVo;
import org.jeecg.modules.tls.blog.article.vo.ArticleVo;
import org.jeecg.modules.tls.blog.article.vo.BlogTagVo;
import org.jeecg.modules.tls.blog.article.vo.PageVo;

import org.jeecg.modules.tls.blog.blogarticletag.entity.BlogArticleTag;
import org.jeecg.modules.tls.blog.blogarticletag.service.IBlogArticleTagService;
import org.jeecg.modules.tls.blog.category.entity.BlogCategory;
import org.jeecg.modules.tls.blog.category.service.IBlogCategoryService;
import org.jeecg.modules.tls.blog.category.vo.CategoryVo;
import org.jeecg.modules.tls.blog.common.AppHttpCodeEnum;
import org.jeecg.modules.tls.blog.common.ResponseResult;

import org.jeecg.modules.tls.blog.tag.entity.BlogTag;

import org.jeecg.modules.tls.blog.tag.service.IBlogTagService;
import org.jeecg.modules.tls.blog.tag.vo.TagVo;
import org.jeecg.modules.tls.blog.utils.BeanCopyUtils;
import org.jeecg.modules.tls.blog.utils.UploadUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: blog_article
 * @Author: jeecg-boot
 * @Date:   2023-01-06
 * @Version: V1.0
 */
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements IBlogArticleService {

    @Autowired
    private IBlogArticleTagService blogArticleTagService;

    @Autowired
    private IBlogTagService blogTagService;

    @Autowired
    private BlogArticleMapper blogArticleMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public ResponseResult rencentBlogList() {
        //查询最近博客，显示最近博客,封装成ResponseResult返回
        LambdaQueryWrapper<BlogArticle> queryWrapper = new LambdaQueryWrapper<>();
        //按照创建时间进行排序
        queryWrapper.orderByDesc(BlogArticle::getCreateTime);
        //最多只查询3条
        Page<BlogArticle> page = new Page(1,3);
        page(page,queryWrapper);
        List<BlogArticle> blogArticles = page.getRecords();
        return ResponseResult.okResult(blogArticles);
    }

    @Override
    public ResponseResult blogArticleList(Integer pageCount,Integer pageSize) {

        //查询条件
        LambdaQueryWrapper<BlogArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        Page<BlogArticle> page = new Page<>(pageCount,pageSize);
        page(page,lambdaQueryWrapper);

        List<BlogArticle> articles = page.getRecords();

        //bean拷贝
        List<ArticleVo> articleVos = new ArrayList<>();
        for (BlogArticle article : articles) {
            ArticleVo vo = new ArticleVo();
            String userId = article.getUserId();
            String categoryId = article.getCategoryId();
            String id = article.getId();

            QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blog_article_id", id);
            List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

            QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
            querySysUserWrapper.eq("id", userId);
            SysUser sysUser = sysUserService.getOne(querySysUserWrapper);

            //获取关联表的的标签id，并且去重
            List<String> blogTagIds = articleTagList.stream()
                    .map(blogArticleTag -> blogArticleTag.getBlogTagId())
                    .collect(Collectors.toList());

            List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);
            //bean拷贝
            List<BlogTagVo> tagVos = new ArrayList<>();
            for (BlogTag tag : blogTags) {
                BlogTagVo blogTagVo = new BlogTagVo();
                blogTagVo.setTagId(tag.getId());
                blogTagVo.setTagName(tag.getName());
                tagVos.add(blogTagVo);
            }
            String categoryName = blogArticleMapper.getCategoryName(categoryId);
            vo.setTags(tagVos);
            vo.setCategoryName(categoryName);
            vo.setAuthor(sysUser.getUsername());
            vo.setAvatar(sysUser.getAvatar());
            BeanUtils.copyProperties(article,vo);
            articleVos.add(vo);
        }

        PageVo pageVo = new PageVo(articleVos,page.getTotal());
        if(pageVo == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.QUERY_BLOG_LIST_ERROR);
        }
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult blogSearch(Integer pageCount, Integer pageSize, String searchCondition) {

        Page<BlogArticle> page = new Page(pageCount,pageSize);
        LambdaQueryWrapper<BlogArticle> wrapper = Wrappers.<BlogArticle>lambdaQuery();
        if(StrUtil.isNotBlank(searchCondition)){
            wrapper.like(BlogArticle::getTitle,searchCondition);
        }
        page(page,wrapper);
        List<BlogArticle> articles = page.getRecords();
        List<ArticleVo> articleVos = new ArrayList<>();
        for (BlogArticle article : articles) {
            ArticleVo vo = new ArticleVo();
            String categoryId = article.getCategoryId();
            String categoryName = blogArticleMapper.getCategoryName(categoryId);
            String userId = article.getUserId();
            String id = article.getId();



            QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
            querySysUserWrapper.eq("id", userId);
            SysUser sysUser = sysUserService.getOne(querySysUserWrapper);
            QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blog_article_id", id);
            List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

            //获取关联表的的标签id，并且去重
            List<String> blogTagIds = articleTagList.stream()
                    .map(blogArticleTag -> blogArticleTag.getBlogTagId())
                    .collect(Collectors.toList());

            List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);
            //bean拷贝
            List<BlogTagVo> tagVos = new ArrayList<>();
            for (BlogTag tag : blogTags) {
                BlogTagVo blogTagVo = new BlogTagVo();
                blogTagVo.setTagId(tag.getId());
                blogTagVo.setTagName(tag.getName());
                tagVos.add(blogTagVo);
            }
            vo.setTags(tagVos);
            vo.setCategoryName(categoryName);
            vo.setAuthor(sysUser.getUsername());
            vo.setAvatar(sysUser.getAvatar());
            BeanUtils.copyProperties(article,vo);
            articleVos.add(vo);
        }
        PageVo pageVo = new PageVo(articleVos,page.getTotal());
        if(pageVo == null){
            ResponseResult.errorResult(AppHttpCodeEnum.QUERY_BLOG_ERROR);
        }
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult myBlog(Integer pageNum, Integer pageSize) {
        //获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        LoginUser userNow = (LoginUser) subject.getPrincipal();
        //查询条件
        QueryWrapper<BlogArticle> queryMyBlogWrapper = new QueryWrapper<>();
        queryMyBlogWrapper.eq("user_id",userNow.getId());
        //分页查询
        Page<BlogArticle> page = new Page<>(pageNum,pageSize);
        page(page,queryMyBlogWrapper);

        List<BlogArticle> articles = page.getRecords();
        //bean拷贝
        List<ArticleVo> articleVos = new ArrayList<>();
        for (BlogArticle article : articles) {
            ArticleVo vo = new ArticleVo();
            String userId = article.getUserId();
            String categoryId = article.getCategoryId();
            String id = article.getId();

            QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blog_article_id", id);
            List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

            QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
            querySysUserWrapper.eq("id", userId);
            SysUser sysUser = sysUserService.getOne(querySysUserWrapper);

            //获取关联表的的标签id，并且去重
            List<String> blogTagIds = articleTagList.stream()
                    .map(blogArticleTag -> blogArticleTag.getBlogTagId())
                    .collect(Collectors.toList());

            List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);
            //bean拷贝
            List<BlogTagVo> tagVos = new ArrayList<>();
            for (BlogTag tag : blogTags) {
                BlogTagVo blogTagVo = new BlogTagVo();
                blogTagVo.setTagId(tag.getId());
                blogTagVo.setTagName(tag.getName());
                tagVos.add(blogTagVo);
            }
            String categoryName = blogArticleMapper.getCategoryName(categoryId);
            vo.setTags(tagVos);
            vo.setCategoryName(categoryName);
            vo.setAuthor(sysUser.getUsername());
            vo.setAvatar(sysUser.getAvatar());
            BeanUtils.copyProperties(article,vo);
            articleVos.add(vo);
        }

        PageVo pageVo = new PageVo(articleVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public Result<String> addAdminBlog(AddBlogDto addBlogDto) {
        //获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        LoginUser userNow = (LoginUser) subject.getPrincipal();

        String categoryId = addBlogDto.getCategoryId();
        String title = addBlogDto.getTitle();

        if(title == null || title.isEmpty()){
            return Result.error("请请填写标题！");
        }else if(userNow == null){
            return Result.error("token已过期，请重新登陆！");
        }else if(categoryId == null || categoryId.isEmpty()){
            return Result.error("请选择博客分类！");
        }else if(addBlogDto.getTags() == null || addBlogDto.getTags().length==0){
            return Result.error("请选择标签！");
        }

        if(count(Wrappers.<BlogArticle>lambdaQuery().eq(BlogArticle::getTitle,title.trim()))>0){
            return Result.error("标题已存在!");
        }


        BlogArticle blogArticle = new BlogArticle();
        blogArticle.setId(addBlogDto.getId());
        blogArticle.setUserId(userNow.getId());
        blogArticle.setTitle(addBlogDto.getTitle());
        blogArticle.setContent(addBlogDto.getContent());
        blogArticle.setSummary(addBlogDto.getSummary());
        blogArticle.setCategoryId(addBlogDto.getCategoryId());
        blogArticle.setCoverImg(addBlogDto.getCoverImg());
        blogArticle.setTopImg(addBlogDto.getTopImg());
        //添加博客
        if(!save(blogArticle)){
            return Result.error("添加失败!");
        }

        String[] tagIds = addBlogDto.getTags();
        ArrayList<BlogArticleTag> articleTagLists = new ArrayList<>();
        for (String tagId : tagIds) {
            BlogArticleTag blogArticleTag = new BlogArticleTag();
            blogArticleTag.setBlogArticleId(blogArticle.getId());
            blogArticleTag.setBlogTagId(tagId);
            articleTagLists.add(blogArticleTag);
        }
        //添加 博客和标签的关联
        blogArticleTagService.saveBatch(articleTagLists);
        return Result.OK("添加成功!");
    }

    @Override
    public Result<String> editAdminBlog(AddBlogDto addBlogDto) {
        //获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        //修改博客
        BlogArticle article = BeanCopyUtils.copyBean(addBlogDto, BlogArticle.class);
        LoginUser userNow = (LoginUser) subject.getPrincipal();

        String id = addBlogDto.getId();
        String categoryId = addBlogDto.getCategoryId();
        if(id == null || id.isEmpty()) {
            return Result.error("参数校验异常！");
        }else if(userNow == null){
            return Result.error("token已过期！");
        }else if(categoryId == null || categoryId.isEmpty()){
            return Result.error("请选择博客分类！");
        }else if(addBlogDto.getTags() == null || addBlogDto.getTags().length==0){
            return Result.error("请选择标签!");
        }

        article.setUserId(userNow.getId());
        article.setCoverImg(addBlogDto.getCoverImg());
        article.setTopImg(addBlogDto.getTopImg());
        updateById(article);
        String[] tagIds = addBlogDto.getTags();
        //如果有id的话先删除
        if(Objects.nonNull(tagIds) && tagIds.length > 0){
            blogArticleTagService.remove(Wrappers.<BlogArticleTag>lambdaQuery().eq(BlogArticleTag::getBlogArticleId,article.getId()));
        }
        ArrayList<BlogArticleTag> articleTagLists = new ArrayList<>();
        for (String tagId : tagIds) {
            BlogArticleTag blogArticleTag = new BlogArticleTag();
            blogArticleTag.setBlogArticleId(article.getId());
            blogArticleTag.setBlogTagId(tagId);
            articleTagLists.add(blogArticleTag);
        }
        //添加 博客和标签的关联
        blogArticleTagService.saveBatch(articleTagLists);
        return Result.ok("编辑成功!");
    }

    @Override
    public ResponseResult getArticleDetail(String id) {

        //根据id查询文章
        BlogArticle blogArticleDetail = getById(id);
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(blogArticleDetail, ArticleDetailVo.class);
        String categoryId = articleDetailVo.getCategoryId();
        String userId = articleDetailVo.getUserId();
        QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
        querySysUserWrapper.eq("id", userId);
        SysUser sysUser = sysUserService.getOne(querySysUserWrapper);

        String categoryName = blogArticleMapper.getCategoryName(categoryId);

        if(categoryName!=null){
            articleDetailVo.setCategoryName(categoryName);
        }
        articleDetailVo.setAuthor(sysUser.getUsername());
        articleDetailVo.setAvatar(sysUser.getAvatar());

        QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_article_id", id);
        List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

        //获取关联表的的标签id，并且去重
        List<String> blogTagIds = articleTagList.stream()
                .map(blogArticleTag -> blogArticleTag.getBlogTagId())
                .collect(Collectors.toList());

        List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);

        //bean拷贝
        List<BlogTagVo> tagVos = new ArrayList<>();
        for (BlogTag tag : blogTags) {
            BlogTagVo blogTagVo = new BlogTagVo();
            blogTagVo.setTagId(tag.getId());
            blogTagVo.setTagName(tag.getName());
            tagVos.add(blogTagVo);
        }

        if (tagVos!=null){
            articleDetailVo.setTags(tagVos);
        }
        return ResponseResult.okResult(articleDetailVo);
    }

    @Override
    public ResponseResult addBlog(AddArticleDto articleDto) throws IOException {

        //获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        LoginUser userNow = (LoginUser) subject.getPrincipal();

        String categoryId = articleDto.getCategoryId();
        String title = articleDto.getTitle();

        if(title == null || title.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_TITLE_ERROR);
        }else if(userNow == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRED);
        }else if(categoryId == null || categoryId.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_BLOG_CATEGORY_ID_ERROR);
        }else if(articleDto.getTagIds() == null || articleDto.getTagIds().length==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_BLOG_TAG_IDS_ERROR);
        }

        MultipartFile coverImg = articleDto.getCoverImg();
        MultipartFile topImg = articleDto.getTopImg();
        String coverImgUrl = UploadUtils.uploadImage(coverImg);
        String topImgUrl = UploadUtils.uploadImage(topImg);


        if(count(Wrappers.<BlogArticle>lambdaQuery().eq(BlogArticle::getTitle,title.trim()))>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.BLOG_TITLE_ERROR);
        }


        BlogArticle blogArticle = new BlogArticle();
        blogArticle.setId(articleDto.getId());
        blogArticle.setUserId(userNow.getId());
        blogArticle.setTitle(articleDto.getTitle());
        blogArticle.setContent(articleDto.getContent());
        blogArticle.setSummary(articleDto.getSummary());
        blogArticle.setCategoryId(articleDto.getCategoryId());
        blogArticle.setCoverImg(coverImgUrl);
        blogArticle.setTopImg(topImgUrl);
        //添加博客
        if(!save(blogArticle)){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_BLOG_ERROR);
        }

        String[] tagIds = articleDto.getTagIds();
        ArrayList<BlogArticleTag> articleTagLists = new ArrayList<>();
        for (String tagId : tagIds) {
            BlogArticleTag blogArticleTag = new BlogArticleTag();
            blogArticleTag.setBlogArticleId(blogArticle.getId());
            blogArticleTag.setBlogTagId(tagId);
            articleTagLists.add(blogArticleTag);
        }
        //添加 博客和标签的关联
        blogArticleTagService.saveBatch(articleTagLists);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult editBlog(AddArticleDto articleDto) throws IOException {
        //获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        //修改博客
        BlogArticle article = BeanCopyUtils.copyBean(articleDto, BlogArticle.class);
        LoginUser userNow = (LoginUser) subject.getPrincipal();

        String id = articleDto.getId();
        String categoryId = articleDto.getCategoryId();
        if(id == null || id.isEmpty()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_VALID_ERROR);
        }else if(userNow == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRED);
        }else if(categoryId == null || categoryId.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_BLOG_CATEGORY_ID_ERROR);
        }else if(articleDto.getTagIds() == null || articleDto.getTagIds().length==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADD_BLOG_TAG_IDS_ERROR);
        }

        article.setUserId(userNow.getId());
        MultipartFile coverImg = articleDto.getCoverImg();
        MultipartFile topImg = articleDto.getTopImg();
        String coverImgContent = UploadUtils.uploadImage(coverImg);
        String topImgContent = UploadUtils.uploadImage(topImg);
        article.setCoverImg(coverImgContent);
        article.setTopImg(topImgContent);
        updateById(article);
        String[] tagIds = articleDto.getTagIds();
        //如果有id的话先删除
        if(Objects.nonNull(tagIds) && tagIds.length > 0){
            blogArticleTagService.remove(Wrappers.<BlogArticleTag>lambdaQuery().eq(BlogArticleTag::getBlogArticleId,article.getId()));
        }
        ArrayList<BlogArticleTag> articleTagLists = new ArrayList<>();
        for (String tagId : tagIds) {
            BlogArticleTag blogArticleTag = new BlogArticleTag();
            blogArticleTag.setBlogArticleId(article.getId());
            blogArticleTag.setBlogTagId(tagId);
            articleTagLists.add(blogArticleTag);
        }
        //添加 博客和标签的关联
        blogArticleTagService.saveBatch(articleTagLists);
        return ResponseResult.okResult();
    }



    @Override
    public ResponseResult blogArticleCategoryList(Integer pageCount, Integer pageSize, String categoryId) {
        //查询条件
        LambdaQueryWrapper<BlogArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 如果 有categoryId 就要查询时要和传入的相同
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId),BlogArticle::getCategoryId,categoryId);

        //分页查询
        Page<BlogArticle> page = new Page<>(pageCount,pageSize);
        page(page,lambdaQueryWrapper);

        List<BlogArticle> articles = page.getRecords();


        //bean拷贝
        List<ArticleVo> articleVos = new ArrayList<>();
        for (BlogArticle article : articles) {
            ArticleVo vo = new ArticleVo();
            String categoryName = blogArticleMapper.getCategoryName(categoryId);

            String id = article.getId();
            String userId = article.getUserId();


            QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
            querySysUserWrapper.eq("id", userId);
            SysUser sysUser = sysUserService.getOne(querySysUserWrapper);

            QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blog_article_id", id);
            List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

            //获取关联表的的标签id，并且去重
            List<String> blogTagIds = articleTagList.stream()
                    .map(blogArticleTag -> blogArticleTag.getBlogTagId())
                    .collect(Collectors.toList());

            List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);

            //bean拷贝
            List<BlogTagVo> tagVos = new ArrayList<>();
            for (BlogTag tag : blogTags) {
                BlogTagVo blogTagVo = new BlogTagVo();
                blogTagVo.setTagId(tag.getId());
                blogTagVo.setTagName(tag.getName());
                tagVos.add(blogTagVo);
            }
            vo.setTags(tagVos);
            vo.setCategoryName(categoryName);
            vo.setAuthor(sysUser.getUsername());
            vo.setAvatar(sysUser.getAvatar());
            BeanUtils.copyProperties(article,vo);
            articleVos.add(vo);
        }


        PageVo pageVo = new PageVo(articleVos,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult blogArticleTagList(Integer pageCount, Integer pageSize, String tagId) {

        //查询条件
        LambdaQueryWrapper<BlogArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        Page<BlogArticle> page = new Page<>(pageCount,pageSize);
        page(page,lambdaQueryWrapper);



        //查询条件
        LambdaQueryWrapper<BlogArticleTag> batlambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 如果 有categoryId 就要查询时要和传入的相同
        batlambdaQueryWrapper.eq(Objects.nonNull(tagId),BlogArticleTag::getBlogTagId,tagId);

        //分页查询
        Page<BlogArticleTag> pageArticleTag = new Page<>(pageCount,pageSize);
        blogArticleTagService.page(pageArticleTag,batlambdaQueryWrapper);

        List<BlogArticleTag> blogArticleTagList = pageArticleTag.getRecords();


        long count = blogArticleTagService.count(batlambdaQueryWrapper);


        //bean拷贝
        List<ArticleVo> articleVos = new ArrayList<>();
        for (BlogArticleTag blogArticleTag:blogArticleTagList) {
            BlogArticle blogArticle = getById(blogArticleTag.getBlogArticleId());
            String userId = blogArticle.getUserId();

            QueryWrapper<SysUser> querySysUserWrapper = new QueryWrapper<>();
            querySysUserWrapper.eq("id", userId);
            SysUser sysUser = sysUserService.getOne(querySysUserWrapper);

            QueryWrapper<BlogArticleTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("blog_article_id", blogArticleTag.getBlogArticleId());
            List<BlogArticleTag> articleTagList = blogArticleTagService.list(queryWrapper);

            //获取关联表的的标签id，并且去重
            List<String> blogTagIds = articleTagList.stream()
                    .map(BlogArticleTag -> BlogArticleTag.getBlogTagId())
                    .collect(Collectors.toList());

            List<BlogTag> blogTags = blogTagService.listByIds(blogTagIds);

            ArticleVo vo = new ArticleVo();
            //bean拷贝
            List<BlogTagVo> tagVos = new ArrayList<>();
            for (BlogTag tag : blogTags) {
                BlogTagVo blogTagVo = new BlogTagVo();
                blogTagVo.setTagId(tag.getId());
                blogTagVo.setTagName(tag.getName());
                tagVos.add(blogTagVo);
            }
            vo.setTags(tagVos);

            String categoryName = blogArticleMapper.getCategoryName(blogArticle.getCategoryId());
            vo.setCategoryName(categoryName);
            vo.setAuthor(sysUser.getUsername());
            vo.setAvatar(sysUser.getAvatar());
            BeanUtils.copyProperties(blogArticle,vo);
            articleVos.add(vo);
        }


        PageVo pageVo = new PageVo(articleVos,count);
        return ResponseResult.okResult(pageVo);


    }
}
